package com.business.web.spring;


import com.auth.domain.T_user;
import com.business.domain.T_goods;
import com.business.domain.T_order;
import com.business.domain.T_order_goods;
import com.business.service.T_goodsService;
import com.business.service.T_orderService;
import com.business.service.T_order_goodsService;
import com.common.db.JsonVO;
import com.common.db.Pg;
import com.common.utils.*;
import com.system.service.LxConstans;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


//import com.workflow.WorkFlowUtils;
//import com.business.mapper.T_order_goodsMapper;

@Controller
@RequestMapping("/t_order_goods")

public class T_order_goodsController {
    private static Logger log = Logger.getLogger(T_order_goodsController.class.getName());

    @Resource
    private T_order_goodsService t_order_goodsService;
    @Resource
    private T_goodsService t_goodsService;
    @Resource
    private T_orderService t_orderService;

    //@Autowired
    //private WorkFlowUtils workFlowUtils;

    //@Resource
    //private T_order_goodsMapper mapper;
    //private int id;
    //private int[] ids;
    private Pg pg;

    /**
     * 1、1获取 T_order_goods信息列表，并且在列表页面展示
     *
     * @param request
     * @return Pg，json格式
     */
    @RequestMapping("/getAllT_order_goods")
    @ResponseBody
    public Pg getAllT_order_goods(HttpServletRequest request) {
        int pageSize = 10;
        int startIndex = 0;
        T_order_goods t_order_goods = new T_order_goods();
        try {
            if (request.getParameter("pageSize") != null) {
                pageSize = BeanUtils.stringToInt("pageSizeFlag", request.getParameter("pageSize"));
            }
            if (request.getParameter("startIndex") != null) {
                startIndex = BeanUtils.stringToInt("startIndexFlag", request.getParameter("startIndex"));
            }
            if (request.getParameter("process_code") != null) { //取得动作流 流程英文名称，后台查询数据关联使用
                t_order_goods.setProcess_code(request.getParameter("process_code"));
            }
            if (request.getParameter("saller_company_name") != null && !"".equals(request.getParameter("saller_company_name"))) {
                t_order_goods.setSaller_company_name(request.getParameter("saller_company_name"));
            }
            if (request.getParameter("buyer_company_name") != null && !"".equals(request.getParameter("buyer_company_name"))) {
                t_order_goods.setBuyer_company_name(request.getParameter("buyer_company_name"));
            }
            if (request.getParameter("creater") != null && !"".equals(request.getParameter("creater"))) {
                t_order_goods.setCreater(request.getParameter("creater"));
            }
            if (request.getParameter("creattime") != null && !"".equals(request.getParameter("creattime"))) {
                t_order_goods.setCreattime(request.getParameter("creattime"));
            }
            if (request.getParameter("updater") != null && !"".equals(request.getParameter("updater"))) {
                t_order_goods.setUpdater(request.getParameter("updater"));
            }
            if (request.getParameter("updatetime") != null && !"".equals(request.getParameter("updatetime"))) {
                t_order_goods.setUpdatetime(request.getParameter("updatetime"));
            }
            if (request.getParameter("status") != null && !"".equals(request.getParameter("status"))) {
                t_order_goods.setStatus(request.getParameter("status"));
            }
            if (request.getParameter("goods_name") != null && !"".equals(request.getParameter("goods_name"))) {
                t_order_goods.setGoods_name(request.getParameter("goods_name"));
            }
            if (request.getParameter("goods_memo") != null && !"".equals(request.getParameter("goods_memo"))) {
                t_order_goods.setGoods_memo(request.getParameter("goods_memo"));
            }
            if (request.getParameter("goods_info") != null && !"".equals(request.getParameter("goods_info"))) {
                t_order_goods.setGoods_info(request.getParameter("goods_info"));
            }
            if (request.getParameter("ext1") != null && !"".equals(request.getParameter("ext1"))) {
                t_order_goods.setExt1(request.getParameter("ext1"));
            }
            if (request.getParameter("ext2") != null && !"".equals(request.getParameter("ext2"))) {
                t_order_goods.setExt2(request.getParameter("ext2"));
            }
            if (request.getParameter("ext3") != null && !"".equals(request.getParameter("ext3"))) {
                t_order_goods.setExt3(request.getParameter("ext3"));
            }
            if (request.getParameter("order_goods_id") != null && !"".equals(request.getParameter("order_goods_id"))) {
                t_order_goods.setOrder_goods_id(new Integer(request.getParameter("order_goods_id")));
            }
            if (request.getParameter("order_id") != null && !"".equals(request.getParameter("order_id"))) {
                t_order_goods.setOrder_id(new Integer(request.getParameter("order_id")));
            }
            if (request.getParameter("startdate") != null && !"".equals(request.getParameter("startdate"))) {
                t_order_goods.setStartdate(request.getParameter("startdate"));
            }
            if (request.getParameter("enddate") != null && !"".equals(request.getParameter("enddate"))) {
                t_order_goods.setEnddate(request.getParameter("enddate"));
            }
            if (request.getParameter("saller") != null && !"".equals(request.getParameter("saller"))) {
                t_order_goods.setSaller(request.getParameter("saller"));
            }
            if (request.getParameter("buyer") != null && !"".equals(request.getParameter("buyer"))) {
                t_order_goods.setBuyer(request.getParameter("buyer"));
            }
            if (request.getParameter("buyer_tel") != null && !"".equals(request.getParameter("buyer_tel"))) {
                t_order_goods.setBuyer_tel(request.getParameter("buyer_tel"));
            }
            if (request.getParameter("saller_tel") != null && !"".equals(request.getParameter("saller_tel"))) {
                t_order_goods.setSaller_tel(request.getParameter("saller_tel"));
            }
            if (request.getParameter("saller_company_address") != null && !"".equals(request.getParameter("saller_company_address"))) {
                t_order_goods.setSaller_company_address(request.getParameter("saller_company_address"));
            }
            if (request.getParameter("good_num") != null && !"".equals(request.getParameter("good_num"))) {
                t_order_goods.setGood_num(new Double(request.getParameter("good_num")));
            }
            if (request.getParameter("kucuntype") != null && !"".equals(request.getParameter("kucuntype"))) {
                t_order_goods.setKucuntype(request.getParameter("kucuntype"));
            }

            t_order_goods.setStartNo(startIndex);
            t_order_goods.setPageSize(pageSize);
            t_order_goods.setEndNo(pageSize);
            List<T_order_goods> findAll = t_order_goodsService.findAll(t_order_goods);
            int totalCount = t_order_goodsService.findCountAll(t_order_goods);
            pg = new Pg(findAll, totalCount, pageSize, startIndex);
            request.setAttribute("t_order_goodsList", findAll);
            request.setAttribute("pg", pg);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("------query t_order_goodsList error:" + e);
        }

        return pg;
    }

    /**
     * 1、3获取 T_order_goods信息列表，并且在列表页面展示
     *
     * @param request
     * @return Pg，json格式
     */
    @RequestMapping("/getAllT_order_goodsONE")
    @ResponseBody
    public T_order_goods getAllT_order_goodsONE(HttpServletRequest request) {
        int pageSize = 10;
        int startIndex = 0;
        List<T_order_goods> findAll = null;
        T_order_goods t_order_goods = new T_order_goods();
        try {
            if (request.getParameter("pageSize") != null) {
                pageSize = BeanUtils.stringToInt("pageSizeFlag", request.getParameter("pageSize"));
            }
            if (request.getParameter("startIndex") != null) {
                startIndex = BeanUtils.stringToInt("startIndexFlag", request.getParameter("startIndex"));
            }
            if (request.getParameter("process_code") != null) { //取得动作流 流程英文名称，后台查询数据关联使用
                t_order_goods.setProcess_code(request.getParameter("process_code"));
            }
            if (request.getParameter("saller_company_name") != null && !"".equals(request.getParameter("saller_company_name"))) {
                t_order_goods.setSaller_company_name(request.getParameter("saller_company_name"));
            }
            if (request.getParameter("buyer_company_name") != null && !"".equals(request.getParameter("buyer_company_name"))) {
                t_order_goods.setBuyer_company_name(request.getParameter("buyer_company_name"));
            }
            if (request.getParameter("creater") != null && !"".equals(request.getParameter("creater"))) {
                t_order_goods.setCreater(request.getParameter("creater"));
            }
            if (request.getParameter("creattime") != null && !"".equals(request.getParameter("creattime"))) {
                t_order_goods.setCreattime(request.getParameter("creattime"));
            }
            if (request.getParameter("updater") != null && !"".equals(request.getParameter("updater"))) {
                t_order_goods.setUpdater(request.getParameter("updater"));
            }
            if (request.getParameter("updatetime") != null && !"".equals(request.getParameter("updatetime"))) {
                t_order_goods.setUpdatetime(request.getParameter("updatetime"));
            }
            if (request.getParameter("status") != null && !"".equals(request.getParameter("status"))) {
                t_order_goods.setStatus(request.getParameter("status"));
            }
            if (request.getParameter("goods_name") != null && !"".equals(request.getParameter("goods_name"))) {
                t_order_goods.setGoods_name(request.getParameter("goods_name"));
            }
            if (request.getParameter("goods_memo") != null && !"".equals(request.getParameter("goods_memo"))) {
                t_order_goods.setGoods_memo(request.getParameter("goods_memo"));
            }
            if (request.getParameter("goods_info") != null && !"".equals(request.getParameter("goods_info"))) {
                t_order_goods.setGoods_info(request.getParameter("goods_info"));
            }
            if (request.getParameter("ext1") != null && !"".equals(request.getParameter("ext1"))) {
                t_order_goods.setExt1(request.getParameter("ext1"));
            }
            if (request.getParameter("ext2") != null && !"".equals(request.getParameter("ext2"))) {
                t_order_goods.setExt2(request.getParameter("ext2"));
            }
            if (request.getParameter("ext3") != null && !"".equals(request.getParameter("ext3"))) {
                t_order_goods.setExt3(request.getParameter("ext3"));
            }
            if (request.getParameter("order_goods_id") != null && !"".equals(request.getParameter("order_goods_id"))) {
                t_order_goods.setOrder_goods_id(new Integer(request.getParameter("order_goods_id")));
            }
            if (request.getParameter("order_id") != null && !"".equals(request.getParameter("order_id"))) {
                t_order_goods.setOrder_id(new Integer(request.getParameter("order_id")));
            }
            if (request.getParameter("startdate") != null && !"".equals(request.getParameter("startdate"))) {
                t_order_goods.setStartdate(request.getParameter("startdate"));
            }
            if (request.getParameter("enddate") != null && !"".equals(request.getParameter("enddate"))) {
                t_order_goods.setEnddate(request.getParameter("enddate"));
            }
            if (request.getParameter("saller") != null && !"".equals(request.getParameter("saller"))) {
                t_order_goods.setSaller(request.getParameter("saller"));
            }
            if (request.getParameter("buyer") != null && !"".equals(request.getParameter("buyer"))) {
                t_order_goods.setBuyer(request.getParameter("buyer"));
            }
            if (request.getParameter("buyer_tel") != null && !"".equals(request.getParameter("buyer_tel"))) {
                t_order_goods.setBuyer_tel(request.getParameter("buyer_tel"));
            }
            if (request.getParameter("saller_tel") != null && !"".equals(request.getParameter("saller_tel"))) {
                t_order_goods.setSaller_tel(request.getParameter("saller_tel"));
            }
            if (request.getParameter("saller_company_address") != null && !"".equals(request.getParameter("saller_company_address"))) {
                //去掉字符“1P、P”，因为这种盘上扫描后会多出来字符“1P或者'P'”，然后导入的订单中没有这2个字符，所以要截掉后再去后台数据数据库查找；否则匹配不到记录；
                //如果有其他的字符将来也需要截掉，需要在此进行修改，并截取；
                String tempsca = request.getParameter("saller_company_address");
                if (tempsca.startsWith("P")) {
                    tempsca = tempsca.substring(1, tempsca.length());
                } else if (tempsca.startsWith("1P")) {
                    tempsca = tempsca.substring(2, tempsca.length());
                } else if (tempsca.contains("&")) {
                    tempsca = tempsca.substring(0, tempsca.indexOf("&"));
                }
                t_order_goods.setSaller_company_address(tempsca);
            }
            if (request.getParameter("good_num") != null && !"".equals(request.getParameter("good_num"))) {
                t_order_goods.setGood_num(new Double(request.getParameter("good_num")));
            }

            t_order_goods.setStartNo(startIndex);
            t_order_goods.setPageSize(pageSize);
            t_order_goods.setEndNo(pageSize);
            findAll = t_order_goodsService.findAll(t_order_goods);
            int totalCount = t_order_goodsService.findCountAll(t_order_goods);
            pg = new Pg(findAll, totalCount, pageSize, startIndex);
            request.setAttribute("t_order_goodsList", findAll);
            request.setAttribute("pg", pg);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("------query t_order_goodsList error:" + e);
        }
        if (findAll.size() > 0) {
            return findAll.get(0);
        } else {
            return null;
        }
    }

    /**
     * 1、2获取 T_order_goods信息列表，并且在列表页面展示，此处只查询购物车中的商品，order_id是空或者null的数据才显示。
     *
     * @param request
     * @return Pg，json格式
     */
    @RequestMapping("/getAllT_order_goodsGWC")
    @ResponseBody
    public Pg getAllT_order_goodsGWC(HttpServletRequest request) {
        int pageSize = 10;
        int startIndex = 0;
        T_order_goods t_order_goods = new T_order_goods();
        try {
            if (request.getParameter("pageSize") != null) {
                pageSize = BeanUtils.stringToInt("pageSizeFlag", request.getParameter("pageSize"));
            }
            if (request.getParameter("startIndex") != null) {
                startIndex = BeanUtils.stringToInt("startIndexFlag", request.getParameter("startIndex"));
            }
            if (request.getParameter("process_code") != null) { //取得动作流 流程英文名称，后台查询数据关联使用
                t_order_goods.setProcess_code(request.getParameter("process_code"));
            }
            if (request.getParameter("saller") != null && !"".equals(request.getParameter("saller"))) {
                t_order_goods.setSaller(request.getParameter("saller"));
            }
            if (request.getParameter("buyer") != null && !"".equals(request.getParameter("buyer"))) {
                t_order_goods.setBuyer(request.getParameter("buyer"));
            }
            if (request.getParameter("creater") != null && !"".equals(request.getParameter("creater"))) {
                t_order_goods.setCreater(request.getParameter("creater"));
            }
            if (request.getParameter("creattime") != null && !"".equals(request.getParameter("creattime"))) {
                t_order_goods.setCreattime(request.getParameter("creattime"));
            }
            if (request.getParameter("updater") != null && !"".equals(request.getParameter("updater"))) {
                t_order_goods.setUpdater(request.getParameter("updater"));
            }
            if (request.getParameter("updatetime") != null && !"".equals(request.getParameter("updatetime"))) {
                t_order_goods.setUpdatetime(request.getParameter("updatetime"));
            }
            if (request.getParameter("status") != null && !"".equals(request.getParameter("status"))) {
                t_order_goods.setStatus(request.getParameter("status"));
            }
            if (request.getParameter("goods_name") != null && !"".equals(request.getParameter("goods_name"))) {
                t_order_goods.setGoods_name(request.getParameter("goods_name"));
            }
            if (request.getParameter("goods_memo") != null && !"".equals(request.getParameter("goods_memo"))) {
                t_order_goods.setGoods_memo(request.getParameter("goods_memo"));
            }
            if (request.getParameter("goods_info") != null && !"".equals(request.getParameter("goods_info"))) {
                t_order_goods.setGoods_info(request.getParameter("goods_info"));
            }
            if (request.getParameter("ext1") != null && !"".equals(request.getParameter("ext1"))) {
                t_order_goods.setExt1(request.getParameter("ext1"));
            }
            if (request.getParameter("ext2") != null && !"".equals(request.getParameter("ext2"))) {
                t_order_goods.setExt2(request.getParameter("ext2"));
            }
            if (request.getParameter("ext3") != null && !"".equals(request.getParameter("ext3"))) {
                t_order_goods.setExt3(request.getParameter("ext3"));
            }
            if (request.getParameter("order_goods_id") != null && !"".equals(request.getParameter("order_goods_id"))) {
                t_order_goods.setOrder_goods_id(new Integer(request.getParameter("order_goods_id")));
            }

            if (request.getParameter("startdate") != null && !"".equals(request.getParameter("startdate"))) {
                t_order_goods.setStartdate(request.getParameter("startdate"));
            }
            if (request.getParameter("enddate") != null && !"".equals(request.getParameter("enddate"))) {
                t_order_goods.setEnddate(request.getParameter("enddate"));
            }
            t_order_goods.setStartNo(startIndex);
            t_order_goods.setPageSize(pageSize);
            t_order_goods.setEndNo(pageSize);
            List<T_order_goods> findAll = t_order_goodsService.findAllGWC(t_order_goods);
            int totalCount = t_order_goodsService.findCountAllGWC(t_order_goods);
            pg = new Pg(findAll, totalCount, pageSize, startIndex);
            request.setAttribute("t_order_goodsList", findAll);
            request.setAttribute("pg", pg);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("------query t_order_goodsList error:" + e);
        }

        return pg;
    }

    /**
     * 2、新增保存  T_order_goods信息并且返回成功或者失败信息json格式结果如：result:[success/error]
     *
     * @param t_order_goods
     * @param request
     * @return
     */
    @RequestMapping("/saveT_order_goods")
    @ResponseBody
    public JsonVO saveT_order_goods(T_order_goods t_order_goods, HttpServletRequest request, HttpServletResponse response) {
        JsonVO vo = new JsonVO();
        vo.setResult("error");
        vo.setJieguo(false);
        t_order_goods.setCreater("" + request.getSession().getAttribute("username"));
        t_order_goods.setCreattime(DateUtil.getCurrDateTimeStand());

        //如果有特殊需求，需要将json对象值转成java实体对象属性值
        try {
            int res = t_order_goodsService.save(t_order_goods);
            log.error("》》》》》》》》》》》》》更新结果数量res=" + res);
            if (res > 0) {
                vo.setResult("success");
                vo.setJieguo(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }

    /**
     * 2.2、新增保存  T_order_goods信息并且跳转至购物侧页面
     *
     * @param goods_id
     * @param request
     * @return
     */
    @RequestMapping("/saveT_order_goodsGWC")
    //@ResponseBody
    public void saveT_order_goodsGWC(String goods_id, String goods_num, String saller, HttpServletRequest request, HttpServletResponse response) {
        String tempBathPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "" + request.getContextPath() + "/";
        JsonVO vo = new JsonVO();
        vo.setResult("success");
        vo.setJieguo(true);

        //判断是否登录，如果session无效，则直接跳转到登录，不保存订单；
        if (request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID) == null || request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID) == "") {
            log.error("------saveT_order_goodsGWC ERROR: session 为空，session=" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID));
            log.error("------saveT_order_goodsGWC ERROR: 保存购物车失败！goods_id=" + goods_id);
            log.error("------saveT_order_goodsGWC ERROR: 保存购物车失败！goods_num=" + goods_num);
            log.error("------saveT_order_goodsGWC ERROR: 保存购物车失败！buyer=" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID));
            vo.setInfo("保存购物车失败，因为session信息不存在，请重新登录后再操作！");
            //return "redirect:"+tempBathPath;
            try {
                response.sendRedirect(tempBathPath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            T_order_goods t_order_goods = new T_order_goods();
            //goods_id可能是多个商品id
            String goodsId[] = goods_id.split(",");
            String tempGoodsId = "0";
            String goodsNum[] = goods_num.split(",");
            String tempGoodsNum = "0";

            for (int k = 0; k < goodsId.length; k++) {
                tempGoodsId = goodsId[k];
                t_order_goods.setGoods_id(new Integer(tempGoodsId));
                if (goods_num.length() > k) {
                    tempGoodsNum = goodsNum[k];
                }
                t_order_goods.setGood_num(new Double(tempGoodsNum));
                //登录整合后，修改为真实的登录账号公司id；
                t_order_goods.setBuyer("" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID));
                t_order_goods.setBuyer_company_name("" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_NAME));
                t_order_goods.setBuyer_company_address("" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ADDRESS));
                t_order_goods.setBuyer_tel("" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_TEL));

                t_order_goods.setCreater("" + request.getSession().getAttribute(LxConstans.SESSION_USER_CUSTOMER_ID));
                t_order_goods.setCreattime(DateUtil.getCurrDateTimeStand());
                //查询商品的一些属性，赋值到订单商品中；

                T_goods goods = new T_goods();
                goods.setGoods_id(new Integer(tempGoodsId));
                goods = t_goodsService.findById(goods);
                t_order_goods.setPrice(new Double(goods.getPrice()));
                t_order_goods.setExt1(""); //买家送货地址，新加了字段，暂时不用了；
                t_order_goods.setExt2(""); //联系电话，新加了字段，暂时不用了
                t_order_goods.setExt3(""); //

                t_order_goods.setSaller(saller);
                t_order_goods.setSaller_company_address(goods.getCustomer_address()); //企业地址
                t_order_goods.setSaller_company_name(goods.getCustomer_name()); //企业名称
                t_order_goods.setSaller_tel(goods.getCompany_name()); //电话

                //如果有特殊需求，需要将json对象值转成java实体对象属性值
                if (t_order_goodsService.save(t_order_goods) <= 0) {
                    vo.setResult("error");
                    vo.setJieguo(false);
                }
            }//end for;

            try {
                response.sendRedirect(tempBathPath + "cart");
            } catch (IOException e) {
                e.printStackTrace();
                log.error(e);
            }
        }

        //return "";
    }

    /**
     * 2.2、一键下单, 一键装机对应的一键下单；
     *
     * @param goods_id
     * @param request
     * @return
     */
    @RequestMapping("/saveT_order_goodsYjxd")
    @ResponseBody
    public JsonVO saveT_order_goodsYjxd(String goods_id, String goods_num, String saller, HttpServletRequest request, HttpServletResponse response) {
        JsonVO vo = new JsonVO();
        vo.setResult("failed");
        vo.setJieguo(false);
        //登录整合后，修改为真实的登录账号公司id；
        if (request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID) == null || request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID) == "") {
            log.error("------saveT_order_goodsYjxd222 ERROR: session 为空，session=" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID));
            log.error("------saveT_order_goodsYjxd222 ERROR: 保存订单失败！goods_id=" + goods_id);
            log.error("------saveT_order_goodsYjxd222 ERROR: 保存订单失败！goods_num=" + goods_num);
            log.error("------saveT_order_goodsYjxd222 ERROR: 保存订单失败！buyer=" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID));
            vo.setInfo("保存订单失败，因为session信息不存在，请重新登录后再操作！");
            return vo;
        }

        T_order_goods t_order_goods = new T_order_goods();
        //goods_id可能是多个商品id
        String goodsId[] = goods_id.split(",");
        String tempGoodsId = "0";
        String goodsNum[] = goods_num.split(",");
        String tempGoodsNum = "0";

        for (int k = 0; k < goodsId.length; k++) {
            tempGoodsId = goodsId[k];
            t_order_goods.setGoods_id(new Integer(tempGoodsId));
            if (goods_num.length() > k) {
                tempGoodsNum = goodsNum[k];
            }
            t_order_goods.setGood_num(new Double(tempGoodsNum));
            t_order_goods.setBuyer("" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID));
            t_order_goods.setBuyer_company_name("" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_NAME));
            t_order_goods.setBuyer_company_address("" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ADDRESS));
            t_order_goods.setBuyer_tel("" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_TEL));

            t_order_goods.setCreater("" + request.getSession().getAttribute(LxConstans.SESSION_USER_CUSTOMER_ID));
            t_order_goods.setCreattime(DateUtil.getCurrDateTimeStand());
            //查询商品的一些属性，赋值到订单商品中；

            T_goods goods = new T_goods();
            goods.setGoods_id(new Integer(tempGoodsId));
            goods = t_goodsService.findById(goods);
            t_order_goods.setPrice(new Double(goods.getPrice()));
            t_order_goods.setExt1(""); //买家送货地址，新加了字段，暂时不用了；
            t_order_goods.setExt2(""); //联系电话，新加了字段，暂时不用了
            t_order_goods.setExt3(""); //

            //t_order_goods.setSaller(saller);
            t_order_goods.setSaller(goods.getCustomer_id());
            t_order_goods.setSaller_company_address(goods.getCustomer_address()); //企业地址
            t_order_goods.setSaller_company_name(goods.getCustomer_name()); //企业名称
            t_order_goods.setSaller_tel(goods.getCompany_name()); //电话

            //如果有特殊需求，需要将json对象值转成java实体对象属性值
            if (t_order_goodsService.save(t_order_goods) <= 0) {
                vo.setResult("error");
                vo.setJieguo(false);
            }
        }
        T_order t_order = new T_order();
        t_order.setBuyer("" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID));
        t_order.setOrder_no("");
        t_order.setCreater("" + request.getSession().getAttribute(LxConstans.SESSION_USER_CUSTOMER_ID));
        t_order.setCreattime(DateUtil.getCurrDateTimeStand());
        t_order.setStatus("0"); //已下单待发货
        t_order.setExt3((String) request.getSession().getAttribute("SESSION_USER_COMPANY_ADDRESS"));
        t_order.setExt2((String) request.getSession().getAttribute("SESSION_USER_COMPANY_NAME"));
        t_order.setExt1((String) request.getSession().getAttribute("SESSION_USER_COMPANY_TEL"));

        if (t_orderService.save(t_order) > 0) {
            vo.setResult("success");
            vo.setJieguo(true);
            //更新购物车中货物订单号为 t_order的order_id;
            T_order_goods goods = new T_order_goods();
            String buyer = "" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID);
            goods.setBuyer(buyer);
            goods.setOrder_id(t_order.getOrder_id());
            goods.setStatus("0"); //0：已下单；1：已确认发货
            //对同一个买方企业购物车中的商品全部更新为订单号，状态为已下单，
            t_order_goodsService.updateByBuyer(goods);

            //对商品销售熟练进行更新增加；对商品库存数量进行更新减少
            T_order_goods temp_order_goods = new T_order_goods();
            temp_order_goods.setOrder_id(t_order.getOrder_id());
            temp_order_goods.setStartNo(0);
            temp_order_goods.setEndNo(100000);
            List list = t_order_goodsService.findAll(temp_order_goods);
            Iterator it = list.iterator();
            while (it.hasNext()) {
                T_order_goods tod = (T_order_goods) it.next();
                T_goods tg = new T_goods();
                tg.setGoods_id(tod.getGoods_id());
                tg.setStartNo(0);
                tg.setEndNo(100000);
                T_goods oldtg = t_goodsService.findById(tg);
                if (oldtg.getSalenum() != null) {
                    oldtg.setSalenum(oldtg.getSalenum() + tod.getGood_num().intValue()); //更新销售数量 +
                } else {
                    oldtg.setSalenum(tod.getGood_num().intValue()); //更新销售数量 +
                }

                oldtg.setNum(oldtg.getNum() - tod.getGood_num().intValue()); //更新库存数量 -
                t_goodsService.update(oldtg);
            }
        } else {
            vo.setResult("error");
            vo.setJieguo(false);
        }
        vo.setResult("success");
        vo.setJieguo(true);
        vo.setInfo("下单成功！");
        //return "";
        return vo;
    }

    /**
     * 2、2\新增保存  T_order信息并且返回成功或者失败信息json格式结果如：result:[success/error]
     *
     * @param goods_id
     * @param request
     * @return
     */
    @RequestMapping("/saveT_order_goodsYJXD")
    //@ResponseBody
    public String saveT_order_goodsYJXD(String goods_id, String goods_num, String saller, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        String tempBathPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "" + request.getContextPath() + "/";
        JsonVO vo = new JsonVO();
        vo.setResult("error");
        vo.setJieguo(false);

        //判断是否登录，如果session无效，则直接跳转到登录，不保存订单；
        if (request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID) == null || request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID) == "") {
            log.error("------saveT_order_goodsYJXD111 ERROR: session 为空，session=" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID));
            log.error("------saveT_order_goodsYJXD111 ERROR: 保存订单失败！goods_id=" + goods_id);
            log.error("------saveT_order_goodsYJXD111 ERROR: 保存订单失败！goods_num=" + goods_num);
            log.error("------saveT_order_goodsYJXD111 ERROR: 保存订单失败！buyer=" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID));
            vo.setInfo("保存订单失败，因为session信息不存在，请重新登录后再操作！");
            return "redirect:" + tempBathPath;
        }

        //如果有特殊需求，需要将json对象值转成java实体对象属性值
        //登录整合后，修改为真实的登录账号公司id；
        T_order t_order = new T_order();
        t_order.setBuyer("" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID));
        t_order.setOrder_no("");
        t_order.setCreater("" + request.getSession().getAttribute(LxConstans.SESSION_USER_CUSTOMER_ID));
        t_order.setCreattime(DateUtil.getCurrDateTimeStand());
        t_order.setStatus("0"); //已下单待发货

        //插入订单表及订单货物信息表
        if (t_orderService.save(t_order) > 0) {
            vo.setResult("success");
            vo.setJieguo(true);

            //插入订单货物表订单号为 t_order的order_id货物信息;
            T_order_goods t_order_goods = new T_order_goods();
            t_order_goods.setOrder_id(t_order.getOrder_id());
            t_order_goods.setStatus("0"); //0：已下单；1：已确认发货

            t_order_goods.setGoods_id(new Integer(goods_id));
            t_order_goods.setGood_num(new Double(goods_num));
            //登录整合后，修改为真实的登录账号公司id；
            t_order_goods.setBuyer("" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID));
            t_order_goods.setBuyer_company_name("" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_NAME));
            t_order_goods.setBuyer_company_address("" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ADDRESS));
            t_order_goods.setBuyer_tel("" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_TEL));

            t_order_goods.setCreater("" + request.getSession().getAttribute(LxConstans.SESSION_USER_CUSTOMER_ID));
            t_order_goods.setCreattime(DateUtil.getCurrDateTimeStand());
            //查询商品的一些属性，赋值到订单商品中；

            T_goods goods = new T_goods();
            goods.setGoods_id(new Integer(goods_id));
            goods = t_goodsService.findById(goods);

            t_order_goods.setPrice(new Double(goods.getPrice()));
            t_order_goods.setExt1(""); //买家送货地址
            t_order_goods.setExt2(""); //联系电话
            t_order_goods.setExt3(""); //

            t_order_goods.setSaller(saller);
            t_order_goods.setSaller_company_address(goods.getCustomer_address()); //企业地址
            t_order_goods.setSaller_company_name(goods.getCustomer_name()); //企业名称
            t_order_goods.setSaller_tel(goods.getCompany_name()); //电话

            //如果有特殊需求，需要将json对象值转成java实体对象属性值
            if (t_order_goodsService.save(t_order_goods) > 0) {
                //更新商品销售数量 与 商品库存数量
                int salenum = 0; //由于salenum初始时为null；
                if (goods.getSalenum() == null) {
                    salenum = 0;
                } else {
                    salenum = goods.getSalenum();
                }
                goods.setSalenum(salenum + t_order_goods.getGood_num().intValue());
                goods.setNum(goods.getNum() - t_order_goods.getGood_num().intValue());
                t_goodsService.update(goods);

                vo.setResult("success");
                vo.setJieguo(true);
            } else {
                vo.setResult("error");
                vo.setJieguo(false);
            }

        }

        try {
            redirectAttributes.addAttribute("order_id", t_order.getOrder_id());

            //response.sendRedirect(tempBathPath+"caigouThree");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e);
        }

        return "redirect:" + tempBathPath + "caigouThree";
    }

    /**
     * 2.3、新增保存  T_order_goods信息但不跳转
     *
     * @param goods_id
     * @param request
     * @return
     */
    @RequestMapping("/saveChooseT_order_goods")
    //@ResponseBody
    public void saveChooseT_order_goods(String goods_id, String goods_num, String saller, HttpServletRequest request, HttpServletResponse response) {
        JsonVO vo = new JsonVO();
        vo.setResult("success");
        vo.setJieguo(true);
        T_order_goods t_order_goods = new T_order_goods();
        //goods_id可能是多个商品id
        String goodsId[] = goods_id.split(",");
        String goodsNum[] = goods_num.split(",");
        String tempGoodsId = "0";
        String tempGoodsNum = "0";

        for (int k = 0; k < goodsId.length; k++) {
            tempGoodsId = goodsId[k];
            t_order_goods.setGoods_id(new Integer(tempGoodsId));
            if (goods_num.length() > k) {
                tempGoodsNum = goodsNum[k];
            }
            t_order_goods.setGood_num(new Double(tempGoodsNum));
            //登录整合后，修改为真实的登录账号公司id；
            //t_order_goods.setBuyer(""+request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID));
            t_order_goods.setBuyer("" + request.getSession().getAttribute(LxConstans.SESSION_USER_COMPANY_ID));
            t_order_goods.setSaller(saller);
            t_order_goods.setCreater("" + request.getSession().getAttribute(LxConstans.SESSION_USER_CUSTOMER_ID));
            t_order_goods.setCreattime(DateUtil.getCurrDateTimeStand());
            //查询商品的一些属性，赋值到订单商品中；

            T_goods goods = new T_goods();
            goods.setGoods_id(new Integer(tempGoodsId));
            goods = t_goodsService.findById(goods);
            t_order_goods.setPrice(new Double(goods.getPrice()));
            t_order_goods.setExt1(""); //买家送货地址
            t_order_goods.setExt2(""); //联系电话
            t_order_goods.setExt3(""); //
            //如果有特殊需求，需要将json对象值转成java实体对象属性值
            if (t_order_goodsService.save(t_order_goods) <= 0) {
                vo.setResult("error");
                vo.setJieguo(false);
            }
        }

        String tempBathPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "" + request.getContextPath() + "/";
        try {
            response.sendRedirect(tempBathPath + "caigou2");
        } catch (IOException e) {
            e.printStackTrace();
            log.error(e);
        }
        //return "";
    }

    /**
     * 3、修改T_order_goods信息并且返回成功或者失败信息json格式结果如：result:[success/error]
     *
     * @param t_order_goods
     * @param request
     * @return JsonVO
     */
    @RequestMapping("/updateT_order_goods")
    @ResponseBody
    public JsonVO updateT_order_goods(T_order_goods t_order_goods, HttpServletRequest request) {
        JsonVO vo = new JsonVO();
        vo.setResult("error");
        vo.setJieguo(false);
        //如果有特殊值需要处理，在此处手工转换
        //T_order_goods t_order_goodsTemp = t_order_goodsService.findById(t_order_goods);
        //t_order_goodsTemp.setXXX(t_order_goods.getXXX());
        //if(t_order_goodsService.update(t_order_goodsTemp)){
        //t_order_goods.setCreattime(DateUtil.getCurrDateTimeStand() ); //yyyy-MM-dd HH:mm:ss
        //t_order_goods.setCreater(sys_user.getUsername());

        //设置操作员为登录账号
        HttpSession session = request.getSession(true);
        T_user sys_user = (T_user) session.getAttribute(SystemConstants.SESSIONNAME_OBJECT);
        t_order_goods.setUpdater(sys_user.getUsername());
        t_order_goods.setUpdatetime(DateUtil.getCurrDateTimeStand()); //yyyy-MM-dd HH:mm:ss

        if (t_order_goodsService.update(t_order_goods) > 0) {
            vo.setResult("success");
            vo.setJieguo(true);
        }
        return vo;
    }


    /**
     * 3、2 修改T_order_goods信息并且返回成功或者失败信息json格式结果如：result:[success/error]
     *
     * @param t_order_goods
     * @param request
     * @return JsonVO
     */
    @RequestMapping("/updateT_order_goodsOut")
    @ResponseBody
    public JsonVO updateT_order_goodsOut(T_order_goods t_order_goods, HttpServletRequest request) {
        JsonVO vo = new JsonVO();
        vo.setResult("error");
        vo.setJieguo(false);
        try {
            //如果有特殊值需要处理，在此处手工转换
            //设置操作员为登录账号
            HttpSession session = request.getSession(true);
            T_user sys_user = (T_user) session.getAttribute(SystemConstants.SESSIONNAME_OBJECT);
            t_order_goods.setUpdater(sys_user.getUsername());
            t_order_goods.setUpdatetime(DateUtil.getCurrDateTimeStand()); //yyyy-MM-dd HH:mm:ss
            t_order_goods.setGood_num(t_order_goods.getGood_num() - t_order_goods.getPrice());
            if (t_order_goodsService.update(t_order_goods) > 0) {
                log.info("------更新领用后库存信息到 t_order_goods表成功，t_order_goods.getOrder_goods_id()=getOrder_goods_id" + t_order_goods.getOrder_goods_id());
                T_order t_order = new T_order();
                t_order.setBuyer(t_order_goods.getBuyer()); //领用人
                t_order.setCreater(sys_user.getUsername());
                t_order.setCreattime(DateUtil.getCurrDateTimeStand());
                t_order.setExt1(t_order_goods.getSaller());  //领用日期
                t_order.setOrder_info(t_order_goods.getBuyer_company_address());  //领用原因
                t_order.setAmount(new BigDecimal(t_order_goods.getPrice().toString())); //领用数量
                t_order.setOrder_no(t_order_goods.getOrder_goods_id().toString());
                t_order.setExt2(t_order_goods.getBuyer_company_name()); //仓库
                t_order.setExt3(t_order_goods.getSaller_tel()); //类别
                t_order.setMomo1(t_order_goods.getBuyer_tel()); //物料编号
                t_order.setMemo2(t_order_goods.getSaller_company_address()); //规格型号
                t_order.setMemo3(t_order_goods.getExt2()); //单位
                t_order.setMemo4(t_order_goods.getExt3()); //生产日期
                t_order.setMemo5(t_order_goods.getExt1()); //批次号
                t_order.setStatus("0"); //0：出库；1：入库，
                t_order.setMemo10("未归还"); //未归还 ， 已归还
                t_order.setBiztype(t_order_goods.getKucuntype()); //wuye,OA;

                int retval = t_orderService.save(t_order);
                if (retval > 0) {
                    vo.setResult("success");
                    vo.setJieguo(true);
                } else {
                    log.error("------插入领用信息到 t_order表失败，请联系管理员，t_order_goods.getOrder_goods_id()=getOrder_goods_id" + t_order_goods.getOrder_goods_id());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }

    /**
     * 3.2、确认发货
     * 修改T_order_goods信息并且返回成功或者失败信息json格式结果如：result:[success/error]
     *
     * @param id
     * @param request
     * @return JsonVO
     */
    @RequestMapping("/t_order_goodsUpdateQrfh")
    @ResponseBody
    public JsonVO t_order_goodsUpdateQrfh(Integer id, HttpServletRequest request) {
        JsonVO vo = new JsonVO();
        vo.setResult("error");
        vo.setJieguo(false);

        T_order_goods t_order_goods = new T_order_goods();
        t_order_goods.setOrder_goods_id(id);
        t_order_goods = t_order_goodsService.findById(t_order_goods);
        t_order_goods.setStatus("1"); //设置状态为已发货
        t_order_goods.setUpdater("" + request.getSession().getAttribute("SESSION_USER_CUSTOMER_ID"));
        t_order_goods.setUpdatetime(DateUtil.getCurrDateTime());

        if (t_order_goodsService.update(t_order_goods) > 0) {
            vo.setResult("success");
            vo.setJieguo(true);
        }
        return vo;
    }


    /**
     * 4、根据id查询T_order_goods单个信息并显示在修改页面
     *
     * @param id
     * @param request
     * @return T_order_goods json格式字符串
     */
    @RequestMapping("/getT_order_goods")
    @ResponseBody
    public T_order_goods getT_order_goods(Integer id, HttpServletRequest request) {
        T_order_goods t_order_goods = new T_order_goods();
        t_order_goods.setOrder_goods_id(id);
        return t_order_goodsService.findById(t_order_goods);
    }

    /**
     * 5、删除T_order_goods根据id
     *
     * @param id
     * @param request
     * @param response return JsonVO ：json格式字符串
     */
    @RequestMapping("/delT_order_goods")
    @ResponseBody
    public JsonVO delT_order_goods(Integer id, HttpServletRequest request, HttpServletResponse response) {
        JsonVO vo = new JsonVO();
        vo.setResult("error");
        vo.setJieguo(false);

        T_order_goods t_order_goods = new T_order_goods();
        t_order_goods.setOrder_goods_id(id);

        if (t_order_goodsService.delete(t_order_goods) > 0) {
            vo.setResult("success");
            vo.setJieguo(true);
        }
        return vo;
    }

    /**
     * 5、2删除T_order_goods根据Buyer
     *
     * @param buyer
     * @param request
     * @param response return JsonVO ：json格式字符串
     */
    @RequestMapping("/delT_order_goodsByBuyer")
    @ResponseBody
    public JsonVO delT_order_goodsByBuyer(String buyer, HttpServletRequest request, HttpServletResponse response) {
        JsonVO vo = new JsonVO();
        vo.setResult("error");
        vo.setJieguo(false);

        T_order_goods t_order_goods = new T_order_goods();
        t_order_goods.setBuyer(buyer);

        if (t_order_goodsService.deleteByBuyer(t_order_goods) > 0) {
            vo.setResult("success");
            vo.setJieguo(true);
        }
        return vo;
    }

    /**
     * 6、跳转到t_order_goods/toT_order_goodsList查询所有数据页面
     * return /WEB-INF/views/business/t_order_goods/t_order_goodsList.jsp
     */
    @RequestMapping(value = "/toT_order_goodsList")
    public String toT_order_goodsList() {
        log.debug("------跳转到/WEB-INF/views/business/t_order_goods/t_order_goodsList.jsp------");
        return "business/t_order_goods/t_order_goodsList";
    }

    /**
     * 7.1、新增、修改、详情页面t_order_goods/toT_order_goodsCurd跳转
     * business/t_order_goods/T_order_goodsCurd.jsp
     */
    @RequestMapping(value = "/toT_order_goodsCurd")
    public String toT_order_goodsCurd() {
        log.debug("------跳转到/WEB-INF/views/business/t_order_goods/t_order_goodsCurd.jsp------");
        return "business/t_order_goods/t_order_goodsCurd";
    }


    /**
     * 7.2、新增、修改、详情页面t_order_goods/toT_order_goodsCurd跳转
     * business/t_order_goods/T_order_goodsCurd.jsp
     */
    @RequestMapping(value = "/toT_order_goodsSMDY")
    public String toT_order_goodsSMDY(HttpServletRequest request) {
        String buyer = request.getParameter("buyer");
        log.debug("------跳转到/WEB-INF/views/business/t_order_goods/t_order_goodsSMDY.jsp------");
        return "business/t_order_goods/t_order_goodsSMDY";
        /**
         if( buyer.equals("1")){
         log.debug("------跳转到/WEB-INF/views/business/t_order_goods/t_order_goodsSMDY.jsp------");
         return "business/t_order_goods/t_order_goodsSMDY";
         } else if( buyer.equals("2")){
         log.debug("------跳转到/WEB-INF/views/business/t_order_goods/t_order_goodsSMDY2.jsp------");
         return "business/t_order_goods/t_order_goodsSMDY2";
         } else if( buyer.equals("3")){
         log.debug("------跳转到/WEB-INF/views/business/t_order_goods/t_order_goodsSMDY3.jsp------");
         return "business/t_order_goods/t_order_goodsSMDY3";
         } else if(buyer.equals("4")){
         log.debug("------跳转到/WEB-INF/views/business/t_order_goods/t_order_goodsSMDY4.jsp------");
         return "business/t_order_goods/t_order_goodsSMDY4";
         } else {
         log.debug("------没有找到值，到默认页面，跳转到/WEB-INF/views/business/t_order_goods/t_order_goodsSMDY.jsp------");
         return "business/t_order_goods/t_order_goodsSMDY";
         }
         //*/
    }

    /**
     * 8、新增、修改、详情页面t_order_goods/toCompany_jf_list跳转
     * business/t_order_goods/toCompany_jf_list.jsp
     */
    @RequestMapping(value = "/toCompany_jf_list")
    public String toCompany_jf_list() {
        log.debug("------跳转到/WEB-INF/views/business/t_order_goods/company_jf_list.jsp------");
        return "business/t_order_goods/company_jf_list";
    }


    /**
     * 8、批量删除T_order_goods根据主键id
     * 是否要实现批量删除，待考虑，基本上用途不大
     * @param id
     * @param request
     * @param response
     @RequestMapping("/delT_order_goodsBatch") public String delT_order_goodsBatch(HttpServletRequest request,HttpServletResponse response){
     String[] ids = request.getParameterValues("ids");
     System.out.println("------ids="+ids);
     T_order_goods t_order_goods = new T_order_goods();
     int j=0;
     for(int i =0;i< ids.length;i++){
     j++;
     log.error("------ids[i]="+ids[i]);
     t_order_goods.setOrder_goods_id(new Integer(ids[i]).intValue());
     t_order_goodsService.delete(t_order_goods);
     }
     if(j==ids.length){
     log.debug("------t_order_goods批量删除成功！------");
     }else{
     log.error("------t_order_goods批量删除部分成功！部分失败！------");
     }

     return "redirect:/t_order_goods/getAllT_order_goods";
     }
     */

    /**
     * 20、跳转至上传excel页面 business/t_order_goods/importExcel.jsp
     * business/t_order_goods/importExcel.jsp
     */
    @RequestMapping(value = "/toImportExcel")
    public String toImportExcel() {
        log.debug("------跳转到/WEB-INF/views/business/t_order_goods/importExcel.jsp------");
        return "business/t_order_goods/importExcel";
    }

    /**
     * 21、跳转至上传excel页面 business/t_order_goods/importKucunExcel.jsp
     * business/t_order_goods/importKucunExcel.jsp
     */
    @RequestMapping(value = "/toImportKucunExcel")
    public String toImportKucunExcel() {
        log.debug("------跳转到/WEB-INF/views/business/t_order_goods/importKucunExcel.jsp------");
        return "business/t_order_goods/importKucunExcel";
    }

    @RequestMapping(value = "/toImportKucunExcelOA")
    public String toImportKucunExcelOA() {
        log.debug("------跳转到/WEB-INF/views/business/t_order_goods/importKucunExcelOA.jsp------");
        return "business/t_order_goods/importKucunExcelOA";
    }

    /**
     * 22、跳转至上传excel页面 business/t_order_goods/importExcel.jsp
     * business/t_order_goods/importExcel.jsp
     */
    @RequestMapping(value = "/toT_order_goodsKucunList")
    public String toT_order_goodsKucunList() {
        log.debug("------跳转到/WEB-INF/views/business/t_order_goods/t_order_goodsKucunList.jsp------");
        return "business/t_order_goods/t_order_goodsKucunList";
    }

    @RequestMapping(value = "/toT_order_goodsKucunListOA")
    public String toT_order_goodsKucunListOA() {
        log.debug("------跳转到/WEB-INF/views/business/t_order_goods/t_order_goodsKucunListOA.jsp------");
        return "business/t_order_goods/t_order_goodsKucunListOA";
    }

    /**
     * 22、跳转至上传excel页面 business/t_order_goodsOutList.jsp
     * business/t_order_goods/importExcel.jsp
     */
    @RequestMapping(value = "/toT_order_goodsOutList")
    public String toT_order_goodsOutList() {
        log.debug("------跳转到/WEB-INF/views/business/t_order_goods/t_order_goodsOutList.jsp------");
        return "business/t_order_goods/t_order_goodsOutList";
    }

    @RequestMapping(value = "/toT_order_goodsOutListOA")
    public String toT_order_goodsOutListOA() {
        log.debug("------跳转到/WEB-INF/views/business/t_order_goods/t_order_goodsOutListOA.jsp------");
        return "business/t_order_goods/t_order_goodsOutListOA";
    }
    /**
     * 23、新增、修改、详情页面t_order_goods/toT_order_goodsOutCurd 跳转
     * business/t_order_goods/toT_order_goodsOutCurd.jsp
     */
    @RequestMapping(value = "/toT_order_goodsOutCurd")
    public String toT_order_goodsOutCurd() {
        log.debug("------跳转到/WEB-INF/views/business/t_order_goods/toT_order_goodsOutCurd.jsp------");
        return "business/t_order_goods/t_order_goodsOutCurd";
    }

    /**
     * 23、新增、修改、详情页面t_order_goods/t_order_goodsKucunCurd 跳转
     * business/t_order_goods/t_order_goodsKucunCurd.jsp
     */
    @RequestMapping(value = "/toT_order_goodsKucunCurd")
    public String toT_order_goodsKucunCurd() {
        log.debug("------跳转到/WEB-INF/views/business/t_order_goods/t_order_goodsKucunCurd.jsp------");
        return "business/t_order_goods/t_order_goodsKucunCurd";
    }

    @RequestMapping(value = "/toT_order_goodsKucunCurdOA")
    public String toT_order_goodsKucunCurdOA() {
        log.debug("------跳转到/WEB-INF/views/business/t_order_goods/t_order_goodsKucunCurdOA.jsp------");
        return "business/t_order_goods/t_order_goodsKucunCurdOA";
    }

    /**
     * 20、跳转至上传excel页面 business/t_order_goods/importExcel.jsp
     * business/t_order_goods/importExcel.jsp
     */
    @RequestMapping(value = "/toT_order_goodsPrintList")
    public String toT_order_goodsPrintList() {
        log.debug("------跳转到/WEB-INF/views/business/t_order_goods/t_order_goodsPrintList.jsp------");
        return "business/t_order_goods/t_order_goodsPrintList";
    }

    @RequestMapping(value = "/toT_order_goodsPrint")
    public String toT_order_goodsPrint(HttpServletRequest request) {
        //需要根据传入的客户模板类型（buyer）字段值去做不同的打印显示页面，受制于打印控件原因，如果同一个页面多个div，
        //隐藏div也会打印出多个空白页，所以需要一个页面只能显示一个div，并且div的id必须是page1\page2\page3这种方式命名，否则打印报错；
        String mbtype = request.getParameter("buyer");
        if ("1".equals(mbtype)) {
            log.debug("------跳转到/WEB-INF/views/business/t_order_goods/toT_order_goodsPrint.jsp------");
            return "business/t_order_goods/t_order_goodsPrint";
        } else if ("2".equals(mbtype)) {
            log.debug("------跳转到/WEB-INF/views/business/t_order_goods/toT_order_goodsPrint2.jsp------");
            return "business/t_order_goods/t_order_goodsPrint2";
        } else if ("3".equals(mbtype)) {
            log.debug("------跳转到/WEB-INF/views/business/t_order_goods/toT_order_goodsPrint3.jsp------");
            return "business/t_order_goods/t_order_goodsPrint3";
        } else if ("4".equals(mbtype)) {
            log.debug("------跳转到/WEB-INF/views/business/t_order_goods/toT_order_goodsPrint4.jsp------");
            return "business/t_order_goods/t_order_goodsPrint4";
        } else {
            log.debug("------跳转到/WEB-INF/views/business/t_order_goods/toT_order_goodsPrint.jsp------");
            return "business/t_order_goods/t_order_goodsPrint";
        }
    }

    /**
     * 20、跳转至上传excel页面 business/t_order_goods/t_order_goodsScanList.jsp
     * business/t_order_goods/importExcel.jsp
     */
    @RequestMapping(value = "/toT_order_goodsScanList")
    public String toT_order_goodsScanList() {
        log.debug("------跳转到/WEB-INF/views/business/t_order_goods/t_order_goodsScanList.jsp------");
        return "business/t_order_goods/t_order_goodsScanList";
    }

    @RequestMapping(value = "/toT_order_goodsRePrint")
    public String toT_order_goodsRePrint() {
        log.debug("------跳转到/WEB-INF/views/business/t_order_goods/t_order_goodsRePrint.jsp------");
        return "business/t_order_goods/t_order_goodsRePrint";
    }


    /**
     * 21、
     * business/t_order_goods/importExcel.jsp
     */
    @RequestMapping("/addExcel2")
    @ResponseBody
    public JsonVO addExcel2(HttpSession session, HttpServletRequest request) {

        log.error("进入上传excel方法：");
        List<T_order_goods> inds = new ArrayList<T_order_goods>();//创建保存数据的list
        JsonVO vo = new JsonVO();
        vo.setResult("error");
        vo.setJieguo(false);

        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            //输入流
            InputStream in = null;
            MultipartFile file = multipartRequest.getFile("upfile");
            if (file.isEmpty()) {
                vo.setResult("error");
                vo.setJieguo(false);
                vo.setInfo("文件为空！");
                return vo;
            }
            String filename = file.getOriginalFilename();
            in = file.getInputStream();
            //得到excel
            Workbook wb = null;
            String fileType = filename.substring(filename.lastIndexOf("."));
            if ((".xls").equals(fileType)) {
                wb = new HSSFWorkbook(in);  //2003-
            } else if ((".xlsx").equals(fileType)) {
                wb = new XSSFWorkbook(in);  //2007+
            } else {
                vo.setResult("error");
                vo.setJieguo(false);
                vo.setInfo("文件格式错误！");
                return vo;
            }
            T_order_goods t_order_goods = null;
            //得到sheet
            Sheet sheet = wb.getSheetAt(0); //默认取第一个sheet
            //int colsNum = sheet.getPhysicalNumberOfRows();  //获取实际的行数
            int rowsNum = sheet.getLastRowNum();//
            for (int j = 1; j < rowsNum + 1; j++) //第一行为表头，所以从第二行开始
            {// getLastRowNum，获取最后一行的行标
                Row row = sheet.getRow(j);
                if (row != null) {
                    t_order_goods = new T_order_goods();
                    t_order_goods.setBuyer(row.getCell(0).toString());
                    t_order_goods.setStatus("0");
                    t_order_goods.setPrice(new Double(0)); //已扫描盘数，默认值设置为0；
                    t_order_goods.setExt1(row.getCell(1).toString());
                    t_order_goods.setExt2(row.getCell(2).toString());
                    //继续处理其他值

                    //inds.add(t_order_goods);
		            /*for (int k = 0; k < row.getLastCellNum(); k++)
		            {// getLastCellNum，是获取最后一个不为空的列是第几个
		                System.out.print(row.getCell(k) + "\t");
		            }*/
                }

            }
            System.out.println(inds);
            wb.close();
        } catch (IOException e) {
            vo.setResult("success");
            vo.setJieguo(true);
            vo.setInfo("遇到代码错误，请联系技术人员！");
            e.printStackTrace();
        }
        //获取登陆用户的id
        //SessionInfo sessionInfo=(SessionInfo)session.getAttribute(GlobalConstant.SESSION_INFO);
        //selfEvaluationService.addExcel(sessionInfo.getId(),inds);

        vo.setResult("success");
        vo.setJieguo(true);

        return vo;
    }

    /**
     * 导入excel
     * 解决Spring boot返回JSON对象后在IE客户端会提示下载JSON文件的问题
     * Spring boot版本1.5.3.RELEASE
     * 问题详细原因:在一个通用上传下载接口中,使用@Responsebody+@RequestMapping(method=RequestMethod.POST, produces = “application/json; charset=UTF-8”),这样返回的为JSON对象
     * 其实这样返回在除IE(又是IE)外的浏览器一般都没有问题,但是如果IE中前端使用iframe框架的话,IE就会将JSON对象当成下载文件(即使是IE11这样支持application/json的也不行),解决这个问题应该有不少办法,网上找了找都不行,最后还是自己尝试着解决,详细过程:
     * ***********最简单的办法，这个方法不返回json对象，只返回字符串即可；
     */
    //@ApiOperation(value = "信息excel数据解析导入数据库", notes = "注意事项")
    //@RequestMapping(value="/excelImport",method= RequestMethod.POST)
    @RequestMapping("/excelKucunUpload")
    //@ResponseBody
    //public JsonVO testexceladd(HttpServletRequest request, HttpServletResponse response, MultipartFile upOrderfile) {
    public String excelKucunUpload(HttpServletRequest request, HttpServletResponse response, MultipartFile upOrderfile) {
        //public JsonVO testexceladd( HttpServletRequest request) {
        // public void saveT_goods(T_goods t_goods, HttpServletRequest request, HttpServletResponse response, @RequestParam("filesZtp") MultipartFile[] files,  @RequestParam("filesXqtp") MultipartFile[] filesXqtp){
        //response.setContentType("text/plain;charset=UTF-8");

        String tempBathPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "" + request.getContextPath() + "/";

        JsonVO vo = new JsonVO();
        vo.setResult("error");
        vo.setJieguo(false);
        List readExcel = null;
        String resultString = "error";

        try {
            //设置操作员为登录账号
            HttpSession session = request.getSession(true);
            T_user sys_user = (T_user) session.getAttribute(SystemConstants.SESSIONNAME_OBJECT);

            //调用util方法拿到解析的数据集合
            readExcel = new ReadExcelUtil().readExcel(upOrderfile);
            //结果如下形式readExcel=[[5, , 7, 8, , ], [, 6, , ]]
            log.error("readExcel=" + readExcel.toString());

            String originalFileName = upOrderfile.getOriginalFilename();
            int beginIndex = originalFileName.lastIndexOf(".");
            //原名
            String fileName = originalFileName.substring(0, beginIndex);
            log.error("fileName=" + fileName.toString() + ";file last=" + originalFileName.substring(beginIndex));
            //excel内容转成vo list；插入表中；
            T_order_goods tod = null;
            //excel row list
            List listVo = readExcel;// excelContentToVo(readExcel);
            if (listVo != null) {
                for (int i = 0; i < listVo.size(); i++) {
                    log.error("lsitVO=" + listVo.get(i).toString());
                    // one row cell value arraylist;
                    List tods = (ArrayList) listVo.get(i);

                    tod = new T_order_goods();
                    //tods 1: 客户名称 buyer_company_name 	2: PO/订单号 Saller_tel	3:CPN/物料编码 Buyer_tel
                    // 4:MPN/规格型号 Saller_company_address	5:QTY/数量 Ext1
                    // 6: BRAND/品牌 ext2	7:供应商 Saller_company_name	8:盘数 Good_num 	 9:辅助出货数量; ext3
                    //其中规格型号 为每个订单唯一的，作为匹配条件；
                    //1:  仓库名称
                    if (tods.size() > 0) {
                        tod.setBuyer_company_name(tods.get(0).toString());
                    }
                    //2:  类别
                    if (tods.size() > 1) {
                        tod.setSaller_tel(tods.get(1).toString());
                    }
                    //3:  物料编码
                    if (tods.size() > 2) {
                        tod.setBuyer_tel(tods.get(2).toString());
                    }
                    //4:  规格型号
                    if (tods.size() > 3) {
                        tod.setSaller_company_address(tods.get(3).toString());
                    }
                    //5:  数量,这个是唯一数值型；
                    if (tods.size() > 4) {
                        tod.setGood_num(new Double(tods.get(4).toString()));
                    }

                    //6:  单位
                    if (tods.size() > 5) {
                        tod.setExt2(tods.get(5).toString());
                    }
                    //7:  供应商
                    if (tods.size() > 6) {
                        tod.setSaller_company_name(tods.get(6).toString());
                    }
                    //8:  批次号
                    if (tods.size() > 7 && tods.get(7) != null && tods.get(7) != "") {
                        tod.setExt1(tods.get(7).toString());
                    }
                    //9:  生产日期;
                    if (tods.size() > 8) {
                        tod.setExt3(tods.get(8).toString());
                    }
                    //10:  预留excel中最后一列
                    if (tods.size() > 9) {
                        tod.setBuyer_company_address(tods.get(9).toString());
                    }
                    tod.setStatus("1"); //导入即生效，status="1"；
                    tod.setCreattime(DateUtil.getCurrDateTimeStand()); //yyyy-MM-dd HH:mm:ss
                    tod.setCreater(sys_user.getUsername());
                    tod.setPrice(new Double(0)); //已扫描盘数，默认值设置为0；
                    tod.setOld_price(new Double(tods.get(4).toString())); //初始化库存数量
                    tod.setKucuntype("wuye"); //OA库存;另外一个 wuye物业库存；
                    //插入数据库
                    t_order_goodsService.save(tod);
                }
            }
            //boolean response = customerInfoService.insterCustomerInfo(readExcel, fileName);
            vo.setResult("success");
            vo.setJieguo(true);
            resultString = "success";
        } catch (Exception e) {
            vo.setResult("false");
            vo.setJieguo(false);
            log.error("readExcel 遇到错误: 请确保excel中‘盘数’列为数字；" + e.toString());
            resultString = "readExcel 遇到错误: 请确保excel中‘盘数’列为数字；\" + e.toString()";
            e.printStackTrace();
        } finally {
            //return vo;
            request.setAttribute("vo", vo);
            request.setAttribute("resultString", resultString);
            return "business/t_order_goods/importKucunExcel";
        }
    }

    /**
     * 导入excel
     * 解决Spring boot返回JSON对象后在IE客户端会提示下载JSON文件的问题
     * Spring boot版本1.5.3.RELEASE
     * 问题详细原因:在一个通用上传下载接口中,使用@Responsebody+@RequestMapping(method=RequestMethod.POST, produces = “application/json; charset=UTF-8”),这样返回的为JSON对象
     * 其实这样返回在除IE(又是IE)外的浏览器一般都没有问题,但是如果IE中前端使用iframe框架的话,IE就会将JSON对象当成下载文件(即使是IE11这样支持application/json的也不行),解决这个问题应该有不少办法,网上找了找都不行,最后还是自己尝试着解决,详细过程:
     * ***********最简单的办法，这个方法不返回json对象，只返回字符串即可；
     */
    //@ApiOperation(value = "信息excel数据解析导入数据库", notes = "注意事项")
    //@RequestMapping(value="/excelImport",method= RequestMethod.POST)
    @RequestMapping("/excelKucunUploadOA")
    //@ResponseBody
    //public JsonVO testexceladd(HttpServletRequest request, HttpServletResponse response, MultipartFile upOrderfile) {
    public String excelKucunUploadOA(HttpServletRequest request, HttpServletResponse response, MultipartFile upOrderfile) {
        //public JsonVO testexceladd( HttpServletRequest request) {
        // public void saveT_goods(T_goods t_goods, HttpServletRequest request, HttpServletResponse response, @RequestParam("filesZtp") MultipartFile[] files,  @RequestParam("filesXqtp") MultipartFile[] filesXqtp){
        //response.setContentType("text/plain;charset=UTF-8");

        String tempBathPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "" + request.getContextPath() + "/";

        JsonVO vo = new JsonVO();
        vo.setResult("error");
        vo.setJieguo(false);
        List readExcel = null;
        String resultString = "error";

        try {
            //设置操作员为登录账号
            HttpSession session = request.getSession(true);
            T_user sys_user = (T_user) session.getAttribute(SystemConstants.SESSIONNAME_OBJECT);

            //调用util方法拿到解析的数据集合
            readExcel = new ReadExcelUtil().readExcel(upOrderfile);
            //结果如下形式readExcel=[[5, , 7, 8, , ], [, 6, , ]]
            log.error("readExcel=" + readExcel.toString());

            String originalFileName = upOrderfile.getOriginalFilename();
            int beginIndex = originalFileName.lastIndexOf(".");
            //原名
            String fileName = originalFileName.substring(0, beginIndex);
            log.error("fileName=" + fileName.toString() + ";file last=" + originalFileName.substring(beginIndex));
            //excel内容转成vo list；插入表中；
            T_order_goods tod = null;
            //excel row list
            List listVo = readExcel;// excelContentToVo(readExcel);
            if (listVo != null) {
                for (int i = 0; i < listVo.size(); i++) {
                    log.error("lsitVO=" + listVo.get(i).toString());
                    // one row cell value arraylist;
                    List tods = (ArrayList) listVo.get(i);

                    tod = new T_order_goods();
                    //tods 1: 客户名称 buyer_company_name 	2: PO/订单号 Saller_tel	3:CPN/物料编码 Buyer_tel
                    // 4:MPN/规格型号 Saller_company_address	5:QTY/数量 Ext1
                    // 6: BRAND/品牌 ext2	7:供应商 Saller_company_name	8:盘数 Good_num 	 9:辅助出货数量; ext3
                    //其中规格型号 为每个订单唯一的，作为匹配条件；
                    //1:  仓库名称
                    if (tods.size() > 0) {
                        tod.setBuyer_company_name(tods.get(0).toString());
                    }
                    //2:  类别
                    if (tods.size() > 1) {
                        tod.setSaller_tel(tods.get(1).toString());
                    }
                    //3:  物料编码
                    if (tods.size() > 2) {
                        tod.setBuyer_tel(tods.get(2).toString());
                    }
                    //4:  规格型号
                    if (tods.size() > 3) {
                        tod.setSaller_company_address(tods.get(3).toString());
                    }
                    //5:  数量,这个是唯一数值型；
                    if (tods.size() > 4) {
                        tod.setGood_num(new Double(tods.get(4).toString()));
                    }

                    //6:  单位
                    if (tods.size() > 5) {
                        tod.setExt2(tods.get(5).toString());
                    }
                    //7:  供应商
                    if (tods.size() > 6) {
                        tod.setSaller_company_name(tods.get(6).toString());
                    }
                    //8:  批次号
                    if (tods.size() > 7 && tods.get(7) != null && tods.get(7) != "") {
                        tod.setExt1(tods.get(7).toString());
                    }
                    //9:  生产日期;
                    if (tods.size() > 8) {
                        tod.setExt3(tods.get(8).toString());
                    }
                    //10:  预留excel中最后一列
                    if (tods.size() > 9) {
                        tod.setBuyer_company_address(tods.get(9).toString());
                    }
                    tod.setStatus("1"); //导入即生效，status="1"；
                    tod.setCreattime(DateUtil.getCurrDateTimeStand()); //yyyy-MM-dd HH:mm:ss
                    tod.setCreater(sys_user.getUsername());
                    tod.setPrice(new Double(0)); //已扫描盘数，默认值设置为0；
                    tod.setOld_price(new Double(tods.get(4).toString())); //初始化库存数量
                    tod.setKucuntype("OA"); //OA库存;另外一个 wuye物业库存；

                    //插入数据库
                    t_order_goodsService.save(tod);
                }
            }
            //boolean response = customerInfoService.insterCustomerInfo(readExcel, fileName);
            vo.setResult("success");
            vo.setJieguo(true);
            resultString = "success";
        } catch (Exception e) {
            vo.setResult("false");
            vo.setJieguo(false);
            log.error("readExcel 遇到错误: 请确保excel中‘盘数’列为数字；" + e.toString());
            resultString = "readExcel 遇到错误: 请确保excel中‘盘数’列为数字；\" + e.toString()";
            e.printStackTrace();
        } finally {
            //return vo;
            request.setAttribute("vo", vo);
            request.setAttribute("resultString", resultString);
            return "business/t_order_goods/importKucunExcelOA";
        }
    }
}









